<?php

//商品管理

class GoodsController extends AuthController{
	private $model;
	
	public function __init(){
		parent::__init();
		$this->model=K('Goods');
	}
	
	public function index(){
		$goodData=$this->model->all();
		$this->assign('goodData',$goodData);
		
		
		$this->display();
	}
	
	//添加商品
	public function add(){
		
		//获取商品分类
		$cateData=K('Category')->all();
		$cateData=Data::tree($cateData,'cname');
		$this->assign('cateData',$cateData);
		//获取所有品牌
		$brandData=K('Brand')->all();
		$this->assign('brandData',$brandData);
		
		if(IS_POST){
			
			//调用模型方法
			if(!$this->model->goodsadd()){
				$this->error($this->model->error);
			}
			$this->success('添加成功',U('Admin/Goods/index'));
		}
		
		
		$this->display();
	}
	
	//异步获取属性与规格
	public function getAttrSpec(){
		if(!IS_AJAX) $this->error('非法请求');
		$tid = Q('post.tid',0,'intval');
		$attrData=K('Attr')->all();
		foreach($attrData as $k => $v){
			$attrData[$k]['tavalue']=explode(',', $v['tavalue']);
		}
		$this->ajax($attrData);
	}
	//上传图片
	public function upload(){
	     $upload = new Upload('Upload/Content/' . date('y/m'));
		 $file = $upload->upload();
		 if (empty($file)) {
		 	$this->ajax('上传失败');
		 } else {
		 	$data = $file[0];
			$this->ajax($data);
		 }
	}
	
	
	//	上传图片
	public function upload_photoList(){
	     $upload = new Upload('Upload/Content/' . date('y/m'));
		 $file = $upload->upload();
		 if (empty($file)) 
		 {
		 	$this->ajax('上传失败');
		 } else {
		 	//上传缩略处理
		 	$data = $file[0]['path'];
		 	$img = new Image();
			//重组文件名
			$max = $file[0]['dir'] . 'max' . $file[0]['filename'] . '.'.$file[0]['ext'];
			$medil = $file[0]['dir'] . 'medil' . $file[0]['filename'] . '.'.$file[0]['ext'];
			$small = $file[0]['dir'] . 'small' . $file[0]['filename'] . '.'.$file[0]['ext'];
			//这里要修改一下框架里的缩略图类
			$pathMax = $img->thumb($data,$max,800,800,5);
			$pathMedil = $img->thumb($data,$medil,350,350,5);
			$pathSmall = $img->thumb($data,$small,50,50,5);
			$data = array(
				'path1' => $pathMax,
				'path2' => $pathMedil,
				'path3' => $pathSmall
			);
			$this->ajax($data);
		 }
	}
	
	
	
	/**
	 * 异步删除图片
	 */
	public function delImg(){
	    $path = Q('post.path');
		//删除图片
		unlink($path);
		
	}
	
	
	
	//修改商品
	public function edit(){
		//获得旧数据
		$gid=Q('get.gid',0,'intval');
		$oldData=K('Goods')->where("gid={$gid}")->find();
		$this->assign('oldData',$oldData);
        //获得所有分类
        $oldCate=K('Category')->all();
		$oldCate=Data::tree($oldCate,'cname');
		$this->assign('oldCate',$oldCate);
		//获得所有品牌
		$oldBrand=K('Brand')->all();
		$this->assign('oldBrand',$oldBrand);
		
		//属性与规格
		//属性
		$tid=Q('get.tid',0,'intval');
		$Attr=M()->join("__goods_attr__ ga JOIN __attr__ a ON ga.taid=a.taid")->where("gid={$gid} AND class=0")->all();
		foreach ($Attr as $k => $v) {
			$Attr[$k]['tavalue']=explode(',', $Attr[$k]['tavalue']);
		}
		
		$this->assign('Attr',$Attr);
		//规格
		$spec=M()->join("__goods_attr__ ga JOIN __attr__ a ON ga.taid=a.taid")->where("gid={$gid} AND class=1")->all();
		foreach ($spec as $k => $v) {
			$spec[$k]['tavalue']=explode(',', $spec[$k]['tavalue']);
		}
		
		$this->assign('spec',$spec);
		
		//图片显示
		$detail=K('Detail')->where("gid={$gid}")->all();
	
		$this->assign('detail',$detail);
		
		if(IS_POST){
			//调用模型方法
			if(!$this->model->goodsedit($gid)){
				$this->error($this->model->error);
			}
			$this->success('修改成功',U('Admin/Goods/index'));
		}
		$this->display();
	}
	
	
	//商品的删除
	public function del(){
		$gid=Q('get.gid');
		$this->model->where("gid={$gid}")->delete();
		K('Detail')->where("gid={$gid}")->delete();
		K('Goodslist')->where("gid={$gid}")->delete();
		K('Goodsattr')->where("gid={$gid}")->delete();
		$this->success('删除成功',U('Admin/Goods/index'));
	}
	
	
}






?>